package com.emapp.lccode.mode

import androidx.lifecycle.MutableLiveData
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.bean.CommTypeKt
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.Field
import com.emapp.lccode.bean.Gongxu
import com.emapp.lccode.bean.Project
import com.emapp.lccode.bean.SelectType
import com.emapp.lccode.bean.UpFileKt
import com.emapp.lccode.http.apiService
import com.emapp.lccode.utils.XtomLogger
import com.emapp.lccode.view.DeviceTypeDialog
import com.emapp.lccode.view.DistrictDialog
import com.emapp.lccode.view.ProjectDeviceTypeDialog
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class ProjectDeviceViewModel : BaseViewModel() {
    var device: MutableLiveData<ResultState<ArrayList<DeviceKt>>> = MutableLiveData()
    var devicelist: MutableLiveData<ResultState<DeviceKt>> = MutableLiveData()
    var deviceAdd: MutableLiveData<ResultState<Any>> = MutableLiveData()
    var fields: MutableLiveData<ResultState<ArrayList<Field>>> = MutableLiveData()
    var file: MutableLiveData<ResultState<UpFileKt>> = MutableLiveData()
    var progressList: MutableLiveData<ResultState<ArrayList<DeviceKt>>> = MutableLiveData()
    var gongxuList: MutableLiveData<ResultState<ArrayList<Gongxu>>> = MutableLiveData()
    var projectProgress: MutableLiveData<ResultState<Project>> = MutableLiveData()
    var myProjectDevicePageList: MutableLiveData<ResultState<DeviceKt>> =
        MutableLiveData()
    var selectType: SelectType = SelectType()
    var selectDistrict: SelectType = SelectType()
    val typeDialog: DeviceTypeDialog by lazy { DeviceTypeDialog() }
    val districtDialog: DistrictDialog by lazy { DistrictDialog() }
    val projectDevicetypeDialog: ProjectDeviceTypeDialog by lazy { ProjectDeviceTypeDialog() }
    var work_type = ""//设备大分类(水)
    var project_id = ""
    private val TAG = this.javaClass.simpleName
    private fun log_e(msg: String?) {
        if (BaseConfig.isDebug) XtomLogger.e(TAG, msg)
    }

    fun getDevice(
        check_status: String,
        page: Int
    ) {
        request(
            {
                apiService.getDeviceList(
                    BaseApplication.instance.adminId,
                    check_status,
                    BaseConfig.page_count.toString(),
                    page.toString()
                )
            },
            devicelist,
            false
        )
    }

    /**
     * 设备属性
     */
    fun getDeviceField(
        device_type: String = "",
    ) {
        val u = BaseApplication.instance.getUser() ?: return
        request({
            apiService.getDeviceField(device_type)
        }, fields, true)
    }

    /**
     * 添加编辑设备
     */
    fun addDevice(infor: DeviceKt, map: Map<String, String>) {
        val u = BaseApplication.instance.getUser() ?: return
        infor.admin_id = u.admin_id
        request({
            apiService.projectDeviceAdd(
                infor.project_id,
                infor.name,
                infor.num_code,
                infor.upload_lnglat,
                infor.json_text,
                infor.city,
                infor.district,
                infor.address,
                infor.work_type,
                infor.device_type,
                "1",
                infor.star_time,
                infor.scene_file_ids,
                infor.admin_id,
                infor.remark,
                infor.build_company,
                map
            )
        }, deviceAdd, true)
    }

    /**
     * 添加编辑设备
     */
    fun updateDevice(infor: DeviceKt, map: Map<String, String>) {
        val u = BaseApplication.instance.getUser() ?: return
        infor.admin_id = u.admin_id
        request({
            apiService.projectDeviceUpdate(
                infor.id,
                infor.id,
                infor.project_id,
                infor.name,
                infor.num_code,
                infor.upload_lnglat,
                infor.json_text,
                infor.city,
                infor.district,
                infor.address,
                infor.work_type,
                infor.device_type,
                infor.build_status,
                infor.star_time,
                infor.buildtime,
                infor.scene_file_ids,
                infor.file_ids,
                infor.admin_id,
                infor.remark,
                infor.build_company,
                map
            )
        }, deviceAdd, true)
    }

    fun upFileDevice(f: File) {
        val builder = MultipartBody.Builder().apply {
            setType(MultipartBody.FORM)
            addFormDataPart("admin_id", BaseApplication.instance.getUser()?.admin_id.toString() ?: "")
            addFormDataPart("file", f.name, f.asRequestBody("image/*".toMediaTypeOrNull()))
        }
        request({ apiService.upFileProjectDevice(builder.build()) }, file, false)
    }
    var file2: MutableLiveData<ResultState<UpFileKt>> = MutableLiveData()
    fun upFileDevice2(f: File) {
        val builder = MultipartBody.Builder().apply {
            setType(MultipartBody.FORM)
            addFormDataPart("admin_id", BaseApplication.instance.getUser()?.admin_id.toString() ?: "")
            addFormDataPart("file", f.name, f.asRequestBody("image/*".toMediaTypeOrNull()))
        }
        request({ apiService.upFileProjectDevice(builder.build()) }, file2, false)
    }
    fun upFileComplete(f: File) {
        val builder = MultipartBody.Builder().apply {
            setType(MultipartBody.FORM)
            addFormDataPart("admin_id", BaseApplication.instance.getUser()?.admin_id.toString() ?: "")
            addFormDataPart("file", f.name, f.asRequestBody("image/*".toMediaTypeOrNull()))
        }
        request({ apiService.upFileProjectDeviceComplete(builder.build()) }, file, false)
    }

    /**
     * 设备详情
     */
    fun getDeviceInfor(work_type: String, id: String) {
        request({
            apiService.getDeviceInfor(work_type, id)
        }, device, true)
    }

    /**
     * 进度上报
     */
    fun addProgress(infor: DeviceKt) {
        val u = BaseApplication.instance.getUser() ?: return
        infor.admin_id = u.admin_id
        request({
            apiService.projectProgressAdd(
                infor.admin_id,
                infor.progress_id,
                infor.id,
                infor.build_status,
                infor.name,
                infor.well_process_id,
                infor.content,
                infor.file_ids,
                infor.materials_file_ids,
                infor.remark
            )
        }, deviceAdd, true)
    }

    /**
     * 进度列表
     */
    fun getProgressList(
        device_id: String = "",
    ) {
        request({
            apiService.getProgressList(BaseApplication.instance.adminId, device_id)
        }, progressList, false)
    }
    /**
     * 进度工序列表
     */
    fun getGongxuList(
        device_id: String = "",
    ) {
        request({
            apiService.gongxuList(BaseApplication.instance.adminId, device_id)
        }, gongxuList, false)
    }

    /**
     * 进度详情
     */
    fun getProgressInfor(id: String) {
        request({
            apiService.getProgressInfor(BaseApplication.instance.adminId, id)
        }, device, true)
    }

    fun getProjectDevicePageList(
        project_id: String,
        device_type: String,
        build_status: String,
        keywords: String,
        page: Int
    ) {
        request(
            {
                apiService.getProjectDevicePageList(
                    BaseApplication.instance.adminId,
                    project_id,
                    device_type,
                    build_status,
                    keywords,
                    BaseConfig.page_count.toString(),
                    page.toString()
                )
            },
            myProjectDevicePageList,
            false
        )
    }

    /**
     * 项目进度列表
     */
    fun getProjectProgressList(
        project_id: String = "",
    ) {
        request({
            apiService.getProjectProgressList(BaseApplication.instance.adminId, project_id)
        }, projectProgress, false)
    }
}